package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.UcMember;
import com.atguigu.serviceedu.client.UcenterClient;
import com.atguigu.serviceedu.entity.EduComment;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.service.EduCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author Return
 * @since 2021-04-12
 */
@Api(description = "用户评论管理")
@RestController
@RequestMapping("/eduservice/comment")
@CrossOrigin
public class EduCommentController {
    @Autowired
    private EduCommentService commentService;
    @Autowired
    private UcenterClient ucenterClient;

    @GetMapping("{page}/{limit}/{courseId}")
    public R getPageComment(@PathVariable Long page,
                            @PathVariable Long limit,
                            @PathVariable String courseId){
        Page<EduComment> commentPage = new Page<>(page,limit);
        Map<String, Object> map = new HashMap<>();

        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.orderByDesc("gmt_create");

        commentService.page(commentPage,wrapper);
        
        List<EduComment> records = commentPage.getRecords();
        long current = commentPage.getCurrent();
        long pages = commentPage.getPages();
        long size = commentPage.getSize();
        long total = commentPage.getTotal();
        boolean hasNext = commentPage.hasNext();
        boolean hasPrevious = commentPage.hasPrevious();

        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return R.ok().data(map);
    }

    @ApiOperation(value = "添加评论")
    @PostMapping("save")
    public R save(@RequestBody EduComment comment, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            return R.error().code(28004).message("请登录");
        }
        comment.setMemberId(memberId);

        UcMember ucMember = ucenterClient.getUcInfoById(memberId);

        comment.setNickname(ucMember.getNickname());
        comment.setAvatar(ucMember.getAvatar());

        commentService.save(comment);

        return R.ok();
    }

    @GetMapping("test/{id}")
    public R getTest(@PathVariable String id){
        UcMember ucMember = ucenterClient.getUcInfoById(id);
        return R.ok().data("item",ucMember);
    }
}

